﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace s7nck
{
    public static class Commons
    {


     // public static  Dictionary<string, Dictionary<string, Dictionary<string, string>>> _dict;

       /* public static void ForEachDataItem(Action<KeyValuePair<string, Dictionary<string, string>>> act)
        {
            ForEachDataItem(true, act);
        }


        public static void ForEachDataItem(bool isEnable, Action<KeyValuePair<string, Dictionary<string, string>>> act)
        {
            foreach (KeyValuePair<string, Dictionary<string, string>> pair in _dict["Dataitems"]
)
            {
                if (!(isEnable && pair.Value.ContainsKey("enabled")) || (pair.Value["enabled"] == "1"))
                {
                    act(pair);
                }
            }
        }*/




        /// <summary>
        /// 获取xmlnode中的属性
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static string getNameAttr(XmlNode node)
        {
            string str2;
            if (node.NodeType == XmlNodeType.Comment)
            {
                str2 = "";
            }
            else
            {
                string str = "";
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    string name = attribute.Name;
                    switch (name)
                    {
                        case "name":
                            {
                                str = attribute.Value;
                            }
                            break;
                    }
                }
                str2 = str;
            }
            return str2;
        }

        /// <summary>
        /// 读取adapterxml内容
        /// </summary>
        /// <param name="xmlpath"></param>
        /// <returns></returns>
        public static Dictionary<string, Dictionary<string, Dictionary<string, string>>> getAdapterXml(string xmlpath)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlpath);
            XmlNodeList list1 = document.GetElementsByTagName("Connect");
            XmlNodeList list2 = document.GetElementsByTagName("Dataitems");
            Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
            if ((list1.Count > 0) && list1[0].HasChildNodes)
            {
                IEnumerator enumerator = list1[0].ChildNodes.GetEnumerator();
                
                    XmlNode current;
                    Dictionary<string, string> dictionary4;
                    goto TR_002F;
                TR_0021:
                    if (!dictionary.ContainsKey(current.Attributes[0].Value))
                    {
                        dictionary.Add(current.Attributes[0].Value, dictionary4);
                    }
                TR_002F:
                    while (true)
                    {
                        if (!enumerator.MoveNext())
                        {
                            break;
                        }
                        current = (XmlNode)enumerator.Current;
                        dictionary4 = new Dictionary<string, string>();
                        if (current.HasChildNodes)
                        {
                            foreach (XmlNode node2 in current.ChildNodes)
                            {
                                if (!dictionary4.ContainsKey(node2.Name))
                                {
                                    dictionary4.Add(node2.Name, node2.InnerText);
                                }
                            }
                        }
                        goto TR_0021;
                    }
                
            }
            Dictionary<string, Dictionary<string, string>> dictionary2 = new Dictionary<string, Dictionary<string, string>>();
            if ((list2.Count > 0) && list2[0].HasChildNodes)
            {
                foreach (XmlNode node3 in list2[0].ChildNodes)
                {
                    if (node3.NodeType == XmlNodeType.Element)
                    {
                        Dictionary<string, string> dictionary5 = new Dictionary<string, string>();
                        foreach (XmlNode node4 in node3.ChildNodes)
                        {
                            if (!dictionary5.ContainsKey(node4.Name))
                            {
                                string innerText = "";
                                if (node4.FirstChild is XmlCDataSection)
                                {
                                    innerText = ((XmlCDataSection)node4.FirstChild).Value;
                                }
                                else
                                {
                                    innerText = node4.InnerText;
                                }
                                dictionary5.Add(node4.Name, innerText);
                            }
                        }
                        string key = Commons.getNameAttr(node3);
                        if (!dictionary2.ContainsKey(key))
                        {
                            dictionary2.Add(key, dictionary5);
                        }
                    }
                }
            }
            return new Dictionary<string, Dictionary<string, Dictionary<string, string>>> {
        {
            "Connect",
            dictionary
        },
        {
            "Dataitems",
            dictionary2
        }
    };
        }


        //ping 功能
        public static bool Ping(string ip, int timeOut, int times)
        {
            bool flag2;
            try
            {
                int num = 0;
                while (true)
                {
                    if (num >= times)
                    {
                        flag2 = false;
                        break;
                    }
                    using (Ping ping = new Ping())
                    {
                        if (ping.Send(ip, timeOut).Status == IPStatus.Success)
                        {
                            flag2 = true;
                            break;
                        }
                    }
                    Thread.Sleep(100);
                    num++;
                }
            }
            catch (Exception ex)
            {
                flag2 = false;
                LogHelper.WriteLog(ex.Message, ex);
            }
            return flag2;
        }


        //根据第n个byte的值截取后面的byte的个数
        public static byte[] getbytes(byte[] array,int n)
        {
            byte[] newarray = new byte[] { };

            try
            {
                newarray = array.Skip(n).Take(array.Length - n).ToArray();
            }
            catch (ArgumentNullException ex)
            {

                LogHelper.WriteLog(ex.Message, ex);
            }
           
            foreach(int a in  newarray){
                Console.WriteLine(a);
            }
            return newarray;

        }

       
    }
}
